import { getOrderCompletionTimeLimitEndNodeOptions } from '@/api/order'
import { ref } from 'vue'

export const useGetCompletionTimeLimitEndNodeOptions = () => {
  const getCompletionTimeLimitEndNodeOptionsLoading = ref(false)
  const getCompletionTimeLimitEndNodeOptionsParam =
    ref<OrderSetAPIRequest.GetCompletionTimeLimitEndNodeOptionsParam>({
      ascriptionType: null,
      departmentId: '',
      id: '',
      limit: 50,
      name: '',
      page: 1,
    })
  const completionTimieLimitEndNodeOptions = ref<
    Array<OrderSetAPIResponse.CompletionTimeLimitEndNodeOption>
  >([])

  const getCompletionTimeLimitEndNodeOptions = async (startNodeId: 1 | 2 | 3) => {
    try {
      getCompletionTimeLimitEndNodeOptionsLoading.value = true
      const res = await getOrderCompletionTimeLimitEndNodeOptions(
        getCompletionTimeLimitEndNodeOptionsParam.value,
        startNodeId,
      )
      getCompletionTimeLimitEndNodeOptionsLoading.value = false
      if (res.data.code === 20000) {
        completionTimieLimitEndNodeOptions.value = res.data.data.rows
      }
      return res.data
    } catch (error) {
      getCompletionTimeLimitEndNodeOptionsLoading.value = false
    }
  }

  return {
    completionTimieLimitEndNodeOptions,
    getCompletionTimeLimitEndNodeOptionsParam,
    getCompletionTimeLimitEndNodeOptionsLoading,
    getCompletionTimeLimitEndNodeOptions,
  }
}
